/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipo.mm.web.actions;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ipo.mm.ejb.facade.institution.InstitutionFacadeLocal;
import org.ipo.mm.web.formbeans.InstitutionSelectBean;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.ipo.mm.ejb.entities.Institution;
import org.ipo.mm.ejb.entities.Representative;
import org.ipo.mm.web.util.ConstantManager;
import org.ipo.mm.web.util.EmailValidator;

/**
 *
 * @author Administrator
 */
public class PrintPostMailInstAction extends org.apache.struts.action.Action {
    
 
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        


        ActionForward forward = null;

        InstitutionSelectBean bmb = (InstitutionSelectBean) form;

        String[] institutions = bmb.getSelectedInstitutions();

        InstitutionFacadeLocal facadeLocal = lookupInstitutionFacade();

      ArrayList<String> mails = new ArrayList<String>();
        for (int i = 0; i < institutions.length; i++) {

            String institutionIdString = institutions[i];

            Institution institution = facadeLocal.find(Integer.parseInt(institutionIdString));
            String emailAddress = institution.getEmail();
            EmailValidator emailValidator = new EmailValidator();
            if (emailAddress != null && !"".equals(emailAddress) && emailValidator.validate(emailAddress)) {
                addMail(mails, emailAddress);
            }
        }
            request.setAttribute(ConstantManager.REQUEST_BULK_EMAIL_ADDRESSES, mails);

            forward = mapping.findForward(ConstantManager.NAVIGATION_SEND_BULK_MAIL_LINKS_INST);

        return forward;
    }
    

          private void addMail(ArrayList<String> mails, String mail) {
        if (mails.size() == 0) {
            String m = "mailto:";
            mails.add(m);
        }
        //get last list
        String mailList = mails.remove(mails.size() - 1);

        //check if length is too much
        if (mailList.length() + mail.length() > 2000) {
            mails.add(mailList);
            String m = "mailto:";
            mailList = m;
        }

        if (mailList.charAt(mailList.length() - 1) != ':') {
            mailList += ";";
        }
        mailList += mail;
        mails.add(mailList);

    }
       private InstitutionFacadeLocal lookupInstitutionFacade() {
        try {
            Context c = new InitialContext();
            return (InstitutionFacadeLocal) c.lookup("java:comp/env/InstitutionFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
